package com.hibernate.hql;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.hibernate.domain.Classes;
import com.hibernate.domain.Course;
import com.hibernate.domain.Student;
import com.hibernate.utils.HibernateUtil;

/**
 * hibernate之hql一对多/多对多到多对多关系测试类
 * @author LC
 *
 */
public class ManyOrOneToManyToManyQueryTest extends HibernateUtil {

	/**
	 * 多对多到多对多关系查询操作
	 * 		查询所有的班级的所有的学生,查询所有的学生选择的所有的课程
	 */
	@Test
	public void testQueryAllClasses_Student_Course_1() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Classes c inner join fetch c.students s inner join fetch s.courses cc" ;
		List<Classes> list = session.createQuery(hql).list() ;
		for (Classes classes : list) {
			System.out.println(classes);
			Set<Student> students = classes.getStudents() ;
			for (Student student : students) {
				System.out.println(student);
				Set<Course> courses = student.getCourses() ;
				for (Course course : courses) {
					System.out.println(course);
				}
			}
		}
		session.close() ;
	}
	
	/**
	 * 一对多到多对多关系查询操作
	 * 		查询指定班级中的所有学生，及学生选择的课程
	 */
	@Test
	public void testQueryOneClasses_AllStudents_AllCourses() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Classes c inner join fetch c.students s inner join s.courses cc where c.cid = ?" ;
		Query query = session.createQuery(hql) ;
		query.setParameter(0, 1L) ;
		List<Object[]> list = query.list() ;
		for (Object[] objects : list) {
			for (int i = 0; i < objects.length; i++) {
				if(objects[i] instanceof Classes) {
					Classes classes = (Classes) objects[i] ;
					System.out.println(classes);
					Set<Student> students = classes.getStudents() ;
					for (Student student : students) {
						System.out.println(student);
						Set<Course> courses = student.getCourses() ;
						for (Course course : courses) {
							System.out.println(course);
						}
					}
				} 
			}
		}
		session.close() ;
	}
	
	/**
	 * 多对一和多对多关系操作
	 * 		查询学生所在班级，及所选课程
	 */
	@Test
	public void testQueryStudents_OneClasses_AllCourses() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Student s inner join fetch s.classes c inner join fetch s.courses cc" ;
		Query query = session.createQuery(hql) ;
		List<Student> list = query.list() ;
		for (Student student : list) {
			System.out.println(student);
			Classes classes = student.getClasses() ;
			System.out.println(classes);
			Set<Course> courses = student.getCourses() ;
			for (Course course : courses) {
				System.out.println(course);
			}
		}
		session.close() ;
	}
	
	/**
	 * 一对一和多对多关系操作
	 * 		查询指定学生所在班级，及所选课程
	 */
	@Test
	public void testQueryOneStudent_OneClasses_AllCourses() {
		Session session = sessionFactory.openSession() ;
		String hql = "from Student s inner join fetch s.classes c inner join fetch s.courses cc where s.sname=:sname" ;
		Query query = session.createQuery(hql) ;
		query.setString("sname", "222") ;
		List<Student> students  = query.list() ;
		for (Student student : students) {
			System.out.println(student);
			Classes classes = student.getClasses() ;
			System.out.println(classes);
			Set<Course> courses = student.getCourses() ;
			for (Course course : courses) {
				System.out.println(course);
			}
		}
		session.close() ;
	}
}
